package cn.tedu.csmall.product.webapi.controller;

import cn.tedu.csmall.common.web.JsonResult;
import cn.tedu.csmall.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.pojo.dto.CategoryBaseSimpleDTO;
import cn.tedu.csmall.pojo.vo.CategoryDetailsVO;
import cn.tedu.csmall.pojo.vo.CategorySimpleListItemVO;
import cn.tedu.csmall.product.service.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/categories",produces = "application/json;charset=utf-8")
@Api(tags = "分类管理模块")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    // ===== 在以下方法的参数前添加@Validated / @Valid注解 =====
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Validated CategoryAddNewDTO categoryAddNewDTO){
        categoryService.addNew(categoryAddNewDTO);
        return JsonResult.ok();

    }


    @GetMapping("/list-by-parent")
    public JsonResult<List<CategorySimpleListItemVO>> listByParentId(Long parentId) {
        // 调用service并将结果封装到JsonResult中
        List<CategorySimpleListItemVO> list = categoryService.listByParentId(parentId);
        return JsonResult.ok(list);
    }

    @GetMapping("/{id}")
    public JsonResult<CategoryDetailsVO> getDetailsById(@PathVariable Long id) {
        CategoryDetailsVO category = categoryService.getDetailsById(id);
        return JsonResult.ok(category);
    }

    /**
     * 根据id禁用类别
     * */
    @GetMapping("/disable/{id}")
    public JsonResult<Void> disableById(@PathVariable Long id){
        categoryService.disableById(id);
        return JsonResult.ok();
    }

    /**
     * 根据id启用类别
     * */
    @GetMapping("/enable/{id}")
    public JsonResult<Void> enable(@PathVariable Long id){
        categoryService.enableById(id);
        return JsonResult.ok();
    }

    /**
     * 根据id隐藏类别
     * */
    @GetMapping("/hiddendisplay/{id}")
    public JsonResult<Void> hiddenDisplay(@PathVariable Long id){
        categoryService.hiddendisplayById(id);
        return JsonResult.ok();
    }

    /**
     * 根据id显示类别
     * */
    @GetMapping("/display/{id}")
    public JsonResult<Void> display(@PathVariable Long id){
        categoryService.displayById(id);
        return JsonResult.ok();
    }

    /**
     * 根据id删除类别
     * */
    @GetMapping("/delete/{id}")
    public JsonResult<Void> deleteById(@PathVariable Long id){
        categoryService.deleteById(id);
        return JsonResult.ok();
    }

    /**
     * 根据类别id(客户提交DTO)修改类别的基本属性
     * */
    @PostMapping("/update/basesimple")
    @ApiOperation(value = "修改类别的基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id",name = "id",
                    example = "1",required =true ),
            @ApiImplicitParam(value = "名称",name = "name",
                    example = "电器",required =true ),
            @ApiImplicitParam(value = "关键词",name = "keywords",
                    example = "家电",required =true ),
            @ApiImplicitParam(value = "排序",name = "sort",
                    example = "65",required =true ),
            @ApiImplicitParam(value = "图标",name = "icon",
                    example = "自定义图标65",required =true ),
    })
    public JsonResult<Void> updatebasesimpleById(CategoryBaseSimpleDTO categoryBaseSimpleDTO){
        categoryService.updateCategoryBaseSimpleById(categoryBaseSimpleDTO);
        return JsonResult.ok();
    }

    /**
     *7. 重建缓存
     * 当请求 /categories/cache/rebuild 路径时，
     * 将重建Redis缓存中的所有类别数据（与缓存预热相同的处理）
     * */
    @PostMapping("/categories/cache/rebuild")
    public JsonResult<Void> reBuildRedisCategory(){
        categoryService.reBuildRedisCategory();
        return JsonResult.ok();
    }
}
